<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018/6/12
 * Time: 15:27
 */

namespace App\Repository;


use App\Http\Controllers\Controller;
use App\Order;

class OrderRepository extends Controller
{
    public function storeOrder(array $data)
    {
        $data['user_id'] = auth()->user()->id;
        return Order::create($data);
    }

    public function indexOrder(int $offset = 0, int $range = 10, array $params = [])
    {
        $query = Order::query()
            ->where('user_id', auth()->user()->id)
            ->when(!empty($params['company']), function ($query) use ($params) {
                $query->where('company', $params['company']);
            })->orderBy('id', 'desc');

        return [
            'offset' => $offset,
            'range'  => $range,
            'total'  => $query->count(),
            'items'  => $query->offset($offset)->limit($range)->get()
        ];
    }

    public function getOrder(int $id){
        return Order::findOrFail($id);
    }

    public function updateOrder(int $id, array $data)
    {
        return Order::findOrFail($id)->update($data);
    }

    public function destroyOrder(int $id)
    {
        return Order::destroy($id);
    }
}